package cn.yuhe.basic.controller;


import cn.yuhe.basic.dto.LoginDto;
import cn.yuhe.basic.exception.BusinessException;
import cn.yuhe.basic.service.ILoginService;
import cn.yuhe.basic.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
* @Description: 前台登录字段
* @Author: Mr.Qiu
* @Version: 1.0
* @Date:
*/
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> map = loginService.account(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    // 退出登录
    @GetMapping("/tuichu")
    public AjaxResult quit(HttpServletRequest request){
        try {
            loginService.tuichu(request);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

    // 获取手机验证码
    @PostMapping("/sendMobileCode")
    public AjaxResult sendMobileCode(@RequestBody LoginDto loginDto){
        try {
            loginService.sendMobileCode(loginDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

    // 验证码登录
    @PostMapping("/codeLogin")
    public AjaxResult codeLogin(@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> tokenDto = loginService.codeLogin(loginDto);
            return AjaxResult.me().setResultObj(tokenDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }



}
